
package org.dyndns.cooland.updater.db;

import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class OUTStore {
	
	private static final String	db_file	= "updater/.settings/updater.db";
	
	public OUTStore () throws ClassNotFoundException {

		Class.forName("org.sqlite.JDBC");
	}
	
	public boolean notConfigured() {

		return !(new File(db_file).exists());
	}
	
	/**
	 * Create default initial database
	 * 
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 */
	public void createDefaultDatabase() throws SQLException,
			ClassNotFoundException {

		// проверяем папку .settings
		File settings = new File("updater/.settings");
		if (!(settings.exists() && settings.isDirectory())){
			if (!settings.mkdir()){
				// error creating settings directory
				throw new SQLException(".settings folder not exists!");
			}
		}
		
		Connection connection = null;
		try{
			connection = DriverManager.getConnection("jdbc:sqlite:" + db_file);
			Statement st = connection.createStatement();
			st
					.executeUpdate("create table settings (id integer not null primary key autoincrement,name string unique not null,value string not null)");
			// st.executeUpdate("create unique index settings_name on settings (name asc)");
			st
					.executeUpdate("insert into settings values (1,'net.connection.timeout','10000') ");
			st
					.executeUpdate("insert into settings values (2,'download.source_and_docs','true') ");
			st
					.executeUpdate("insert into settings values (3,'download.show_only_latest','true') ");
			st
					.executeUpdate("insert into settings values (4,'download.hide_installed','false') ");
			st
					.executeUpdate("insert into settings values (5,'download.group_by_category','true') ");
			st
					.executeUpdate("insert into settings values (6,'sites.autoinclude','true') ");
			st
					.executeUpdate("create table sites (id integer not null primary key autoincrement, "
							+ "name string unique not null, "
							+ "url string unique not null, "
							+ "description string, "
							+ "enabled integer not null default 1)");
			// st.executeUpdate("create index sites_name on sites (name asc)");
			st
					.executeUpdate("insert into sites values (1,'localhost', 'http://localhost/orbiter', 'Test site on localhost', 1)");
			// TODO create a main database schema
			st
					.executeUpdate("create table category (id integer not null primary key autoincrement, name string, parent_id integer default 0)");
			st
					.executeUpdate("create table addons_category (id integer not null primary key autoincrement, addons_id integer, category_id integer)");
			
			st
					.executeUpdate("create table authors (id integer not null primary key autoincrement, nickname string, email string, web string)");
			st
					.executeUpdate("create table addons_authors (id integer not null primary key autoincrement, addons_id integer, authors_id integer)");
			
			st
					.executeUpdate("create table compatibility (id integer not null primary key autoincrement, build string)");
			st
					.executeUpdate("create table addons_compatibility (id integer not null primary key autoincrement, addons_id integer, compatibility_id integer)");
			
			st
					.executeUpdate("create table required (id integer not null primary key autoincrement, name string, version string)");
			st
					.executeUpdate("create table addons_required (id integer not null primary key autoincrement, addons_id integer, required_id integer)");
			
			st
					.executeUpdate("create table optional (id integer not null primary key autoincrement, name string, version string)");
			st
					.executeUpdate("create table addons_optional (id integer not null primary key autoincrement, addons_id integer, optional_id integer)");
			
			st
					.executeUpdate("create table incompatible (id integer not null primary key autoincrement, name string, version string)");
			st
					.executeUpdate("create table addons_incompatible (id integer not null primary key autoincrement, addons_id integer, incompatible_id integer)");
			
			st
					.executeUpdate("create table license (id integer not null primary key autoincrement, name string, description string)");
			st
					.executeUpdate("create table addons_license (id integer not null primary key autoincrement, addons_id integer, license_id integer)");
			
			st
					.executeUpdate("create table addons (id integer not null primary key autoincrement, "
							+ "site_id integer, "
							+ "category_id integer,"
							+ "name string, "
							+ "version string, "
							+ "description string, "
							+ "source string, "
							+ "size integer, " + "checksum string)");
			st.close();
		}
		catch (SQLException e){
			e.printStackTrace();
		}
		finally{
			try{
				if (connection != null){
					connection.close();
				}
			}
			catch (SQLException e){
				e.printStackTrace();
			}
		}
		
	}
}
